import Form from './Form.js'
import Sidebar from './Sidebar.js'

export default {
    inject:['version'],
    components: {
        Sidebar,
        Form,
    },
    data() {
        return {
            request: {
                method:  "GET",
                schema:  "HTTPS",
                urlstring: "",
                data: "",
                content_type: "application/json",
                custom_content_type: "",
            },
        }
    },
    template:
    `   
        <div class="flexbox wrap-mobile">
            <Sidebar @getSavedRequest="getSavedRequest" @getNewRequest="getNewRequest"/>
            
            <div class="content blank">
                <div class="article wider">
                    <div class="article-body custom-py-20">
                        <h1 v-if="request.id">REST запрос</h1>
                        <h1 v-else>Новый запрос</h1>
                        <Form :request="request" />
                    </div>
                </div>
            </div>
        </div>
    `,
    methods: {
        getSavedRequest(requestId) {
            console.log('getsaved from main');
            this.request = {
                id: requestId,
                method: "POST",
                schema: "HTTPS",
                urlstring: "bbb.ru",
                data: "{c:3, d:4}",
                content_type: "application/x-www-form-urlencoded",
                custom_content_type: "",
            };

            // let postdata = {
            //     requestId: requestId,
            //     _csrf: this.csrf,
            // };

            // fetch(this.urls.getSavedRequest, {
            //         method: "post",
            //         headers: {
            //             'Accept': 'application/json',
            //             'Content-Type': 'application/x-www-form-urlencoded'
            //         },
            //         body: new URLSearchParams(postdata).toString()
            //     })
            //     .then((response) => {
            //         if (response.ok) {
            //             return response.json();
            //         }
            //         throw new Error('Network response was not ok');
            //     })
            //     .then((json) => {
            //         console.log(json);
            //         this.request = json;
            //     })
            //     .catch((error) => {
            //         console.log(error);
            //     });
        },
        getNewRequest()  {
            console.log("get new");
            this.request = {
                method: "GET",
                schema: "HTTPS",
                urlstring: "",
                data: "",
                content_type: "application/json",
                custom_content_type: "",
            };
        }
    },
}